package com.hello.controller;


import com.hello.dto.PetcardDTO;
import com.hello.dto.UserDTO;
import com.hello.entity.User;
import com.hello.result.Result;
import com.hello.service.IPetCardService;

import com.hello.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/petcard")
@Slf4j
@Api(tags = "宠物卡接口")
public class PetcardController {

    @Resource
    private IPetCardService petCardService;
    @Autowired
    private IUserService userService;

    /**
     * 新增宠物卡
     * @param petcardDTO
     * @return
     */
    @PostMapping("/addPet")
    @ApiOperation(value = "新增宠物卡")
    public Result addPet(@RequestBody PetcardDTO petcardDTO){
        log.info("新增宠物卡:{}",petcardDTO);
        System.out.println("当前线程id:" + Thread.currentThread().getId());
        boolean result = petCardService.addPetCard(petcardDTO);
        if(result){
            UserDTO userDTO = userService.searchById(petcardDTO.getUserId());
            userDTO.setPetNum(userDTO.getPetNum()+1); //用户宠物数量+1
            userService.updateUser(userDTO);
            return Result.success(userDTO);
        }else{
            return Result.error("新增宠物失败");
        }
    }

    @GetMapping("/queryPetCardList/{id}")
    @ApiOperation(value = "宠物信息")
    public Result queryPetCardList(@PathVariable Long id){
        List<PetcardDTO> petcardDTOS = petCardService.queryPetCardList(id);
        //select Petcard by id
        //List<PetcardDTO> petcardDTOSs;
        return Result.success(petcardDTOS);
    }

    @PostMapping("/deletePet/{id}")
    @ApiOperation(value = "删除宠物卡")
    public Result deletePet(@PathVariable Long id){
        log.info("删除宠物卡");
        boolean result  = petCardService.delPetCardById(id);
        if(result) return Result.success();
        else return Result.error("删除宠物失败");
    }
    @PostMapping("/myPet/{user_id}")
    @ApiOperation(value="全部宠物")
    public Result myPet(@PathVariable Long user_id){
        List<PetcardDTO> petcardDTO = petCardService.myPetCard(user_id);
        return Result.success(petcardDTO);
    }


}
